<template>
  <div style="margin-top:15px" >
    <!-- <el-form>
      <el-button type="primary" ref="querybtn" @click="chartToShow()">点我看看</el-button>
    </el-form> -->
    <el-select
      v-model="statisticObj.type"
      style="margin-bottom: 15px"
      @change="showDataByType(statisticObj.type)"
      clearable
      placeholder="请选择"
    >
      <el-option label="近7天" value="sevenDays" />
      <el-option label="近30天" value="aMonth" />
    </el-select>
    <div class="chart-container">
      <div id="chart" class="chart" style="height: 500px; width: 100%" />
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
import statisticsApi from "@/api/statistics";
export default {
  name: "topRank",
  data() {
    return {
      chart: null,
      // videoViewData: {
      //   view: "",
      //   videoTitle: "",
      // },
      videoViewData: [],
      statisticObj: {
        type: "",
      },
    };
  },
  //设置监听事件
  watch: {
    videoViewData: {
      // deep:true,
      handler: function () {
        // console.log(this);
        this.chartToShow();
      },
    },
  },
  created() {
    // this.VideoViewData();
  },
  //   mountedBefore(){
  //       this.VideoViewData();
  //   },
  mounted() {
    this.VideoViewData();
    // setTimeout(()=>{
    //     this.chartToShow()
    // },800)
    // console.log("mount",(this.videoViewData));
    // this.$nextTick( ()=>{this.chartToShow()});
  },
  methods: {
    showDataByType(type) {
      this.videoViewData = [];
      if (type == "") {
        this.VideoViewData();
        return;
      }
      statisticsApi.getVideoViewDataByType(type).then((response) => {
        for (let item of response.data.videoViewData) {
          this.videoViewData.push({ value: item.view, name: item.videoTitle });
        }
        console.log(this.videoViewData);
      });
    },
    VideoViewData() {
      statisticsApi.getVideoViewData().then((response) => {
        //   console.log(response.data)
        for (let item of response.data.videoViewData) {
          this.videoViewData.push({ value: item.view, name: item.videoTitle });
        }
        console.log(this.videoViewData);
      });
    },
    chartToShow() {
      // 基于准备好的dom，初始化echarts实例
      console.log("chartToShow", this.videoViewData);
      this.chart = echarts.init(document.getElementById("chart"));
      // var app = {};
      var option = {
        title: {
          // text: "视频播放top10",
          text:
            this.statisticObj.type == "sevenDays"
              ? "近7天视频播放top10"
              : this.statisticObj.type == "aMonth"
              ? "近30天视频播放top10"
              : "总视频播放top10",
          //   top:"25",
          left: "63%",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
        },
        series: [
          {
            type: "pie",
            radius: "70%",
            height: "92%",
            left: "38%",
            data: this.videoViewData,
            // data: [
            //   { value: 1048, name: "Search Engine" },
            //   { value: 735, name: "Direct" },
            //   { value: 580, name: "Email" },
            //   { value: 484, name: "Union Ads" },
            //   { value: 300, name: "Video Ads" },
            // ],
            itemStyle: {
              borderColor: "#fff",
              borderWidth: 1,
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };

      this.chart.setOption(option);
    },
  },
};
</script>

<style scoped>
</style>
